package com.sanpu.kxe.business.card.presenter;

import android.content.DialogInterface;
import android.text.TextUtils;

import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.card.contract.CreditCardListContract;
import com.sanpu.kxe.business.card.model.CardModel;
import com.sanpu.kxe.business.repay.provider.IRepayProvider;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.module.dialog.GlobalDialog;
import com.sanpu.kxe.rxservice.RxError;
import com.sanpu.kxe.utils.PublicProviderUtil;
import com.sanpu.kxe.utils.ToastUtil;

import java.net.SocketTimeoutException;
import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：信用卡列表presenter
 * Created by 薛勇军 on 2019/5/27.
 */

public class CreditCardListPresenter implements CreditCardListContract.Presenter {

    private CreditCardListContract.View mView;

    private CompositeSubscription mSubscription;

    public CreditCardListPresenter(CreditCardListContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void selectUrmBankPage() {
        Subscription subscription = CardModel.getInstance().selectUrmBankPage(CommonConfig.CREDIT_CARD_TYPE)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<BankInfoOutput>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof RxError) {
                            RxError error = (RxError) e;
                            ToastUtil.showTextViewPrompt(error.getMessage());
                        } else if (e instanceof SocketTimeoutException) {
                            ToastUtil.showTextViewPrompt("网络连接超时!");
                        } else {
                            ToastUtil.showTextViewPrompt("网络连接异常!");
                        }
                        mView.showViewData(null);
                    }

                    @Override
                    public void onNext(List<BankInfoOutput> bankInfoOutputs) {
                        mView.showViewData(bankInfoOutputs);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void updateCardClick(BankInfoOutput infoOutput) {
        IRepayProvider repayProvider = PublicProviderUtil.getProvider(IRepayProvider.class);
        if (repayProvider != null) {
            repayProvider.openUpdateCreditActivity(mView.getContext(), infoOutput);
        }
    }

    @Override
    public void queryCardClick(BankInfoOutput infoOutput) {
        IRepayProvider repayProvider = PublicProviderUtil.getProvider(IRepayProvider.class);
        if (repayProvider != null) {
            repayProvider.openQueryPlanNewActivity(mView.getContext(), infoOutput.getBankId());
        }
    }

    @Override
    public void refundCardClick(final BankInfoOutput infoOutput) {
        if (infoOutput != null) {
            if (TextUtils.isEmpty(infoOutput.getBillDay())) {
                GlobalDialog dialog = new GlobalDialog(mView.getContext())
                        .title("温馨提示")
                        .content("请先设置账单日与还款日");
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        updateCardClick(infoOutput);
                    }
                });
                dialog.show();
            } else {
                IRepayProvider repayProvider = PublicProviderUtil.getProvider(IRepayProvider.class);
                if (repayProvider != null) {
                    repayProvider.openRepayChannelActivity(mView.getContext(), infoOutput);
                }
            }
        }
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
